﻿package sedie 
{
	import flash.display.MovieClip;
	import flash.media.Video;
	import flash.text.TextField;
	import game.AccountInfoPanel;
	import game.BetStatusPanel;
	import game.ChannelPanel;
	import game.CountDownPanel;
	import game.events.EventData;
	import game.exchangeddata.HashTable;
	import game.exchangeddata.TableInfo;
	import game.GameReportPanel;
	import game.GameRulePanel;
	import game.HotLinePanel;
	import game.interfaces.IGameView;
	import game.VideoControl;
	import game.WelcomeMessagePanel;
	import sedie.exchangeddata.BetInfo;
	import sedie.exchangeddata.HistoryInfo;
	import game.constants.ConstData;
	import common.Utility;
	import game.GlobalVariables;
	import game.BetPlace;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class SedieGameView extends MovieClip implements IGameView
	{
		public var welcomeMsg:WelcomeMessagePanel;
		public var tableInfoPanel:TableInfoPanel;
		public var userLiveBetPoolPanel:UserLiveBetPoolPanel;
		public var historyPanel:HistoryPanel;
		public var countDownPanel:CountDownPanel;
		public var betController:BetController;
		public var winLosePanel:WinLosePanel;
		public var cardPanel:CardPanel;
		public var txtBalanceTf:TextField;
		public var balanceTf:TextField;
		public var txtTotalBetTf:TextField;
		public var totalBetTf:TextField;
		public var txtUserTf:TextField;
		public var userTf:TextField;
		private var isInitBetController:Boolean = false;
		//public var flvControl:FLVPlayback;
		public var flvControl:Video;
		public var videoControl:VideoControl;
		public var maskVideo:MovieClip;
		public var channelBottomPanel:sedie.ChannelPanel;
		public var channelTopPanel:sedie.ChannelPanel;
		public var soundList:SoundList;
		public var volumeGame:VolumeController;
		public var betStatusPanel:BetStatusPanel;
		public var gameReportPanel:GameReportPanel;
		public var gameRulePanel:GameRulePanel;
		public var hotLinePanel:HotLinePanel;
		public var currency_mc:MovieClip;
		public var accountInfoPanel:AccountInfoPanel;
		public function SedieGameView() 
		{
			this.betController.gameType = ConstData.GAME_TYPE_SEDIE;
			this.betController.addEventListener(EventData.BET_CHANGE, betChangeHandler);
			this.channelTopPanel.channel.addEventListener(EventData.UPDATE_VIDEO, hdlTopPanel);
			this.channelBottomPanel.channel.addEventListener(EventData.UPDATE_VIDEO, hdlBottonPanel);
			init();
		}
		
		private function hdlBottonPanel(e:EventData):void 
		{
			this.channelTopPanel.channelActived = 	this.channelBottomPanel.channelActived;
			this.channelTopPanel.channelActiving = this.channelBottomPanel.channelActiving;
			this.channelTopPanel.setChannelActiving(this.channelTopPanel.channelActiving, this.channelTopPanel.getYellowColor);
			this.channelTopPanel.setChannelActiving(this.channelTopPanel.channelActived, this.channelTopPanel.getWhiteColor);
		}
		
		private function hdlTopPanel(e:EventData):void 
		{
			this.channelBottomPanel.channelActived = this.channelTopPanel.channelActived;
			this.channelBottomPanel.channelActiving = this.channelTopPanel.channelActiving;
			this.channelBottomPanel.setChannelActiving(this.channelBottomPanel.channelActiving, this.channelBottomPanel.getYellowColor);
			this.channelBottomPanel.setChannelActiving(this.channelBottomPanel.channelActived, this.channelBottomPanel.getWhiteColor);
			
		}
		
		
		
		private function init():void {
			this.videoControl = new VideoControl();
			this.videoControl.addEventListener(EventData.PLAY_VIDEO, setStream);
			
			this.channelBottomPanel.initChannel(2);
			this.channelBottomPanel.gotoAndStop(2);
			this.soundList.stopSound();
			
		}
		private function setStream(e:EventData):void 
		{
			this.flvControl.attachNetStream(videoControl.stream_ns);
			videoControl.stream_ns.play(videoControl.streamName);
		}
		private function betChangeHandler(e:EventData):void {
			updateBalance(e.newBalance);			
			updateTotalBet(this.betController.getTotalBetValue());
		}	
		public function updateGameHis(historyInfo:*):void {
			this.historyPanel.initHis(HistoryInfo(historyInfo).history);
		}
		public function updateGameCard(cardPos:String, cardCode:String):void {
			
		}
		public function updateTableInfo(tableInfo:TableInfo):void {
			this.tableInfoPanel.updateTableInfo(tableInfo);
		}
		public function updateDateClock(tableInfo:TableInfo):void {
			this.tableInfoPanel.updateDateClock(tableInfo);
		}
		
		public function updateTableMinMax(minBet:Number, maxBet:Number):void {
			this.tableInfoPanel.updateTableMinMax(minBet, maxBet);
		}
		public function updateLiveBetPoolGame(liveBetPoolGame:*):void {}
		public function updateLiveBetPoolUserPublic(liveBetPoolUser:*):void {
			this.userLiveBetPoolPanel.updateLiveBetPoolUser(liveBetPoolUser);
		}
		public function updateLiveBetPoolUserPrivate():void{}
		public function updateGameHisPlayerBanker(playerBankerInfo:*, type:String, sLoop:String):void{}
		public function updateGameHisAsk(sLoop:String):void{}
		public function activeGameRule(nameTxt:String):void {
			if(this.gameRulePanel.txtRuleTf.text =="")
			{	
				this.gameRulePanel.loadTxt(nameTxt);
			}
			this.gameRulePanel.initText(GlobalVariables.langInfo.hashTable);
			this.gameRulePanel.show();
		}
		public function activeHotLine():void {
			trace("activeHotLine");
			this.hotLinePanel.initText(GlobalVariables.langInfo.hashTable);
			this.hotLinePanel.show();
		}
		public function updateVideoPath(flvSource:String):void {
			//trace("flvControl:" + flvControl);
			if(flvControl!=null)
			{	
				//trace("flvControl111111111:");
				this.removeChild(flvControl);
				flvControl = null;
			}
				//trace("flvControl222222222:");
				this.flvControl = videoControl.updateVideoPath(flvSource, flvControl, maskVideo.x, maskVideo.y, maskVideo.width, maskVideo.height );
				this.addChild(this.flvControl);
				this.flvControl.mask = maskVideo;
				
				var indexflvControl:int = getChildIndex(flvControl);
				var indexTopPanel:int = getChildIndex(channelTopPanel);
				var indexBottonPanel:int = getChildIndex(channelBottomPanel);
				var indexSoundList:int = getChildIndex(soundList);
				var indexVolumeGame:int = getChildIndex(this.volumeGame);
				var indexMin:int = Utility.compare(indexTopPanel, indexBottonPanel, indexSoundList);
				//trace("indexflvControl:" + indexflvControl +"indexChannelPanel" + indexTopPanel +"indexBottonPanel" + indexBottonPanel +"indexSoundList:"+ indexSoundList + "indexMax:" + indexMax);
				if (indexVolumeGame >= indexMin)
				{
					setChildIndex(flvControl, indexMin - 1);
				}
				else {
					setChildIndex(flvControl, indexVolumeGame - 1);
				}
		}
		
		public function updateChipBetting(minBet:Number, maxBet:Number, currentBalance:Number, initChipValueArr:Array):void {
			trace(updateChipBetting);
			this.updateBalance(currentBalance);
			this.totalBetTf.text = "0";
			if (!this.isInitBetController) {
				this.isInitBetController = true;    
				this.betController.initBet(minBet, maxBet, currentBalance, initChipValueArr);
			}	

		}				
		public function countDownBetting(currentBettingTime:int):void {
			this.countDownPanel.startCountDown(currentBettingTime);
			this.betController.unlock();
		}
		public function showReport():void {
			this.gameReportPanel.initText(GlobalVariables.langInfo.hashTable);
			this.gameReportPanel.show();
		}
		public function updateDateReport(dateFrom:String, dateTo:String):void {
			this.gameReportPanel.updateDate(dateFrom, dateTo);
		}
		public function sendDateReportInfo(position:int):String {
			if (position == 1)
			{
				return this.gameReportPanel.txtDateFromTi.text;
			}
			else
			{
				return this.gameReportPanel.txtDateToTi.text;
			}
		}
		public function updateReport(info:Object):void {
			this.gameReportPanel.updateValueReport(info);
		}
		public function updateValueBetStatus(info:Object):void {
			this.betStatusPanel.updateValueBetStatus(info);
		}
		public function showBetStatus():void {
			trace("showBetStatus");
			this.betStatusPanel.show();
		}
			
		public function initStopGame():void {			
			this.countDownPanel.stopCountDown();
			this.betController.lock();
			if (GlobalVariables.autoAcceptBetting == "0") {
				updateBetting();	
				if (this.betController.getTotalBetValue() == 0) {
					showBetSuccess(true, false);					
				} else {					
					showBetSuccess(true, true);
				}
			}
		}
		
		public function initStartGame(countDownSecond:int):void {			
			countDownBetting(countDownSecond);		
			this.userLiveBetPoolPanel.initLivebetPoolUser();
			this.winLosePanel.init();
			//When start game clear all history bet
			this.betController.clearStartBet();
			this.showBetSuccess(false, false);
			this.totalBetTf.text = "0";
			//When start game reset betting value for betInfo & saveBetInfo
			this.betController.saveBetInfo.resetValue();
			this.betController.betInfo.resetValue();			
			this.cardPanel.hideAllCards();
		}
		
		public function confirmBetting():String {			
			var strConfirmBettingCode:String = "";
			var strConfirmBettingValue:String = "";			
			this.betController.saveBetting();
			if(this.betController.saveBetInfo.getTotalBet() == 0){			
				for (var i:int = 0; i < this.betController.betInfo.arrBettingCode.length; i++) {
					if (this.betController.betInfo.arrBettingValue[i] > 0) {
						strConfirmBettingCode += this.betController.betInfo.arrBettingCode[i] + ",";
						strConfirmBettingValue += this.betController.betInfo.arrBettingValue[i] + ",";
					}
				}			
			} else {
				for (i = 0; i < this.betController.betInfo.arrBettingCode.length; i++) {
					if (this.betController.betInfo.arrBettingValue[i] > 0 
						&& this.betController.betInfo.arrBettingValue[i] > this.betController.saveBetInfo.arrBettingValue[i]) {
						strConfirmBettingCode += this.betController.betInfo.arrBettingCode[i] + ",";
						strConfirmBettingValue += (this.betController.betInfo.arrBettingValue[i] - this.betController.saveBetInfo.arrBettingValue[i]) + ",";
					}
				}			
			}	
			if (strConfirmBettingCode == "")
				return "";
			
			return (strConfirmBettingCode.substr(0, strConfirmBettingCode.length -1) + "@" 
					+ strConfirmBettingValue.substr(0, strConfirmBettingValue.length -1));
		}
		public function clearBetting(e:EventData):void {
			updateBettingChip(this.betController.saveBetInfo);	
			
			this.updateBalance(e.newBalance);
			//this.balanceTf.text = e.newBalance.toString();
			updateTotalBet(this.betController.betInfo.getTotalBet());
		}	
		public function rebetBetting(e:EventData):void {			
			var tmp:sedie.exchangeddata.BetInfo = new sedie.exchangeddata.BetInfo();
			var pos:int = -1;
			for (var i:int = 0; i < this.betController.betInfo.arrBettingCode.length; i++) {
				tmp.arrBettingValue[i] = this.betController.saveBetInfo.arrBettingValue[i] + this.betController.preBetInfo.arrBettingValue[i];		
				if (tmp.arrBettingValue[i] > 0)
				{	
					pos = i;
					trace("pos:" + pos);
				}
			}
			trace("rebetBetting:" + tmp.arrBettingValue[pos] + "pos:" + pos +"tien bet" + e.newBalance);
			this.betController.posBetted.bet = this.betController.hitArr[pos];
			this.betController.posBetted.val = tmp.arrBettingValue[pos];
			updateBettingChip(tmp);
			this.updateBalance(e.newBalance);
			//this.balanceTf.text = e.newBalance.toString();
			this.totalBetTf.text = (this.betController.betInfo.getTotalBet() + this.betController.preBetInfo.getTotalBet()).toString();	
		}
		
		public function saveBettingSuccess():void {
			this.betController.saveBetInfo.copy(this.betController.betInfo);
		}	
		
		
		public function updateBetting():void {	
			this.betController.currentBalance = this.betController.currentBalance + this.betController.getTotalBetValue() - this.betController.saveBetInfo.getTotalBet();

			this.updateBalance(this.betController.currentBalance);
		
			this.totalBetTf.text = this.betController.saveBetInfo.getTotalBet().toString();			
			//update chip when stop game
			updateBettingChip(this.betController.saveBetInfo);
			
			//save preBetInfo to rebet for next game
			if(this.betController.saveBetInfo.getTotalBet() > 0) {
				this.betController.preBetInfo.copy(this.betController.saveBetInfo);			
			}
			
		}
		
		private function updateBettingChip(obj:BetInfo):void {				
			for (var i:int = 0; i < this.betController.betArr.length; i++) {
				BetPlace(this.betController.betArr[i]).setBetValue(obj.arrBettingValue[i]);
				BetPlace(this.betController.betArr[i]).updateChips();
			}
		}
		public function updateCurrUserBet(obj:Object):void {
			this.betController.updateBet(obj);			
			this.betController.saveBetInfo.resetValue();
			if (this.betController.betInfo.getTotalBet() > 0) {
				this.betController.saveBetInfo.copy(obj);
				//update balance							
				this.totalBetTf.text = this.betController.saveBetInfo.getTotalBet().toString();					
				this.betController.showBetSuccess(true, true);
	        } else {
				this.betController.showBetSuccess(true, false);	
	        }			
		}
        public function updateWelcomeMessage(strMsg:String):void {
			//trace("strMsg:" +strMsg);
			this.welcomeMsg.startWelcome(strMsg);
		}
		public function showWinLose(result:String):void {
			this.winLosePanel.showWinLose(result);
			this.cardPanel.initCards(result);
		} 
		public function unLockAsk():void{}
		public function lockAsk():void{}
		public function updatePosActiveRoom(tableNo:String){}
		public function checkActiveRoom():Boolean {
			return true;
		}
		public function initGame():void {
			this.countDownPanel.stopCountDown();
		}
		public function updateGameTxt(hashTableInfo:HashTable):void {
			this.txtTotalBetTf.text = hashTableInfo.lookupString("totalBet");			
			this.txtBalanceTf.text = hashTableInfo.lookupString("MNbalance");
			this.txtUserTf.text = hashTableInfo.lookupString("MNwelcome");
			this.historyPanel.initText(hashTableInfo);
			this.userLiveBetPoolPanel.initText(hashTableInfo);
			this.tableInfoPanel.initText(hashTableInfo);
			this.betController.initText(hashTableInfo);
		}
		public function updateBalance(balance:Number):void {
			//this.balanceTf.text = balance.toString();
			this.balanceTf.text = Utility.currency(balance, 2, "");	
			this.betController.currentBalance = balance;
		}
		
		private function updateTotalBet(balance:Number):void {
			this.totalBetTf.text = Utility.currency(balance, 2, "");	
		}
		
		public function rightBetting():void {			
			var tmp:sedie.exchangeddata.BetInfo = new sedie.exchangeddata.BetInfo();
			for (var i:int = 0; i < this.betController.betArr.length; i++) {												
				if (this.betController.hitArr[i].name == GlobalVariables.betPlaceActive) {	
					if ((BetPlace(this.betController.betArr[i]).getBetValue() - this.betController.saveBetInfo.arrBettingValue[i]) >= GlobalVariables.chipActiveAmount) {
						tmp.arrBettingValue[i] = BetPlace(this.betController.betArr[i]).getBetValue() - GlobalVariables.chipActiveAmount;
						
						if(tmp.arrBettingValue[i] > 0) {
							this.betController.tooltip_mc.tooltip_txt.text = tmp.arrBettingValue[i].toString();
						}
						
						this.betController.currentBalance += GlobalVariables.chipActiveAmount;
						this.updateBalance(this.betController.currentBalance);
						updateTotalBet((this.betController.getTotalBetValue() - GlobalVariables.chipActiveAmount));						
					} else {
						tmp.arrBettingValue[i] = this.betController.saveBetInfo.arrBettingValue[i];
						
						var event:EventData;
						event = new EventData(EventData.SHOW_MS, true);
						event.ms = "changeMsg";
						dispatchEvent(event);
					}
				} else {
					tmp.arrBettingValue[i] = BetPlace(this.betController.betArr[i]).getBetValue();
				}
			}
			updateBettingChip(tmp);
		}
		
		public function setChipActive(chipName:String):void{}
	
		public function showBetSuccess(isShow:Boolean, isBet:Boolean):void {			
			this.betController.showBetSuccess(isShow, isBet);
		}
		public function stopVideo():void {
			this.videoControl.stopVideo();
				
		} 
		public function updateTablesClose(listRoom:Array):void { }
		
		public function initListChannel(data:Array, nameFmsChannel:String):void {
			this.channelTopPanel.initListChannel(data);
			this.channelBottomPanel.initListChannel(data);
			this.channelTopPanel.setChannelActiving(nameFmsChannel, this.channelTopPanel.getYellowColor);
			this.channelBottomPanel.setChannelActiving(nameFmsChannel, this.channelTopPanel.getYellowColor);
			
			this.channelTopPanel.channelActiving = nameFmsChannel;
			this.channelBottomPanel.channelActiving = nameFmsChannel;
		}
		public function updateUserName(info:String):void {
			this.userTf.text = info;
		}
		
		public function initCurrency(currency:String):void {			
			switch(currency) {
				case "USD":
					this.currency_mc.gotoAndStop("USD");
					break;
				
				case "SGD":
					this.currency_mc.gotoAndStop("SGD");
					break;
					
				case "HKD":
					this.currency_mc.gotoAndStop("HKD");
					break;
				
				case "TWD":
					this.currency_mc.gotoAndStop("TWD");
					break;
					
				case "CAD":
					this.currency_mc.gotoAndStop("CAD");
					break;			
					
				case "PHP":
					this.currency_mc.gotoAndStop("PHP");
					break;
				
				case "KRW":
					this.currency_mc.gotoAndStop("KRW");
					break;
					
				case "THB":
					this.currency_mc.gotoAndStop("THB");
					break;
				
				case "CNY":
					this.currency_mc.gotoAndStop("CNY");
					break;
					
				case "MYR":
					this.currency_mc.gotoAndStop("MYR");
					break;
				
				case "EUR":
					this.currency_mc.gotoAndStop("EUR");
					break;
					
				case "PTS":
					this.currency_mc.gotoAndStop("USD");
					break;
				
				case "JPY":
					this.currency_mc.gotoAndStop("JPY");
					break;
					
				case "IDR":
					this.currency_mc.gotoAndStop("IDR");
					break;
				
				case "GBP":
					this.currency_mc.gotoAndStop("GBP");
					break;
					
				default:
					this.currency_mc.gotoAndStop("defaultCur");
					break;
			}
		}
		
		public function initAccountInfo(info:Object):void {
			this.accountInfoPanel.show();
			this.accountInfoPanel.initText(GlobalVariables.langInfo.hashTable);
			this.accountInfoPanel.updateValueAccountInfo(info);
		}
		
		public function showBetIns(betIns:String, betRate:String):void{}
		public function startIns(coundDownSecond:int, betIns:String):void{}	
		public function setBacBetControlStatus(flag:Boolean):void{}
		public function setChipNavPos(chipPos:int):void{};
		public function setFutureRoute(result:String, type:String):void{};
	}

}